﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKCodeBase.TopologySuite;
using YJKRevitCode.RevitTopologySuite;

namespace SectionSignAndFilled.StairsRoomSection.Kit
{
	public class NTSUtility
	{
		public static List<List<CurveLoop>> MergaeArea(List<List<Curve>> inputCurves)
		{
			List<RevitPolygonDataBean> list = new List<RevitPolygonDataBean>();
			foreach (List<Curve> list2 in inputCurves)
			{
				list.Add(new RevitPolygonDataBean(CurveLoop.Create(list2), null));
			}
			LinkedList<RevitPolygonDataBean> linkedList = new LinkedList<RevitPolygonDataBean>();
			LinkedList<RevitPolygonDataBean> linkedList2 = new LinkedList<RevitPolygonDataBean>(list);
			while (linkedList2.Count<RevitPolygonDataBean>() > 0)
			{
				LinkedListNode<RevitPolygonDataBean> node = new LinkedListNode<RevitPolygonDataBean>(linkedList2.First.Value);
				linkedList.AddFirst(node);
				linkedList2.RemoveFirst();
				NTSUtility.MergeAreas(linkedList, linkedList2);
			}
			List<List<CurveLoop>> list3 = new List<List<CurveLoop>>();
			foreach (RevitPolygonDataBean revitPolygonDataBean in linkedList)
			{
				CurveLoop shellCurveLoop = revitPolygonDataBean.ShellCurveLoop;
				List<CurveLoop> lstHolesCurveLoop = revitPolygonDataBean.LstHolesCurveLoop;
				List<CurveLoop> list4 = new List<CurveLoop>();
				list4.Add(shellCurveLoop);
				if (lstHolesCurveLoop != null)
				{
					list4.AddRange(lstHolesCurveLoop);
				}
				list3.Add(list4);
			}
			return list3;
		}

		private static void MergeAreas(LinkedList<RevitPolygonDataBean> PolygonResult, LinkedList<RevitPolygonDataBean> PolygonTmp)
		{
			LinkedListNode<RevitPolygonDataBean> linkedListNode = PolygonResult.First;
			LinkedListNode<RevitPolygonDataBean> linkedListNode2 = PolygonTmp.First;
			RevitPolygonRelateOperater revitPolygonRelateOperater = new RevitPolygonRelateOperater();
			while (PolygonTmp.Count > 0)
			{
				linkedListNode2 = PolygonTmp.First;
				int num = 0;
				while (linkedListNode2 != null)
				{
					RevitPolygonDataBean value = linkedListNode.Value;
					RevitPolygonDataBean value2 = linkedListNode2.Value;
					TopologyRelateKindEnum topologyRelateKindEnum;
					bool flag = revitPolygonRelateOperater.TryGetPolygonRelate(value, value2, out topologyRelateKindEnum);
					if (!flag || topologyRelateKindEnum == TopologyRelateKindEnum.Disjoint)
					{
						linkedListNode2 = linkedListNode2.Next;
					}
					else
					{
						List<RevitPolygonDataBean> list = null;
						RevitPolygonBooleanOperater revitPolygonBooleanOperater = new RevitPolygonBooleanOperater();
						flag = revitPolygonBooleanOperater.TryGetBooleanResult(value, value2, BooleanOperatorKindEnum.Union, out list);
						if (!flag || list.Count != 1)
						{
							linkedListNode2 = linkedListNode2.Next;
						}
						else
						{
							num++;
							PolygonResult.AddLast(list.First<RevitPolygonDataBean>());
							LinkedListNode<RevitPolygonDataBean> next = linkedListNode.Next;
							PolygonResult.Remove(linkedListNode);
							linkedListNode = next;
							LinkedListNode<RevitPolygonDataBean> next2 = linkedListNode2.Next;
							PolygonTmp.Remove(linkedListNode2);
							linkedListNode2 = next2;
						}
					}
				}
				if (num == 0)
				{
					return;
				}
			}
		}
	}
}
